using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace MSRI.DesktopAgent.CeRapi
{
    [StructLayout(LayoutKind.Sequential)]
    internal struct PROCESS_INFORMATION
    {
        public IntPtr hProcess;
        public IntPtr hThread;
        public int dwProcessID;
        public int dwThreadID;
    }
    public class HelperClass
    {
        public static bool StartSmartPhoneClient()
        {
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
            string ProcessPath = GetSmartPhoneClient();
            int response = -1;
            UnmanagedAPI.CeRapiInit();
            if (ProcessPath == "")
                response = UnmanagedAPI.CeCreateProcess("\\Program Files\\SMS Server Client\\SMS Server Client.exe", String.Empty, IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, ref pi);
            else
                response = UnmanagedAPI.CeCreateProcess(ProcessPath, String.Empty, IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, ref pi);
            UnmanagedAPI.CeRapiUninit();
            return response > 0;
        }

        public static Int16 GetTcpPort()
        {
            uint key;
            uint ret;
            int lpType = new int();
            byte[] lpData = new byte[32];
            int lpcbData = 32;
            Int16 Port = -1;
            UnmanagedAPI.CeRapiInit();
            ret = UnmanagedAPI.CeRegOpenKeyEx(UnmanagedAPI.HKEY_LOCAL_MACHINE, "Software\\MSRI\\", 0, 1, out key);
            if (ret == 0)
            {
                ret = UnmanagedAPI.CeRegQueryValueEx(key, "TcpPort", 0, ref lpType, lpData, ref lpcbData);
                if (ret == 0)
                    Port = Convert.ToInt16(System.Text.Encoding.Unicode.GetString(lpData, 0, lpcbData).Replace("\0",""));
            }
            UnmanagedAPI.CeRegCloseKey(key);
            UnmanagedAPI.CeRapiUninit();
            return Port;
        }

        public static String GetSmartPhoneClient()
        {
            uint key;
            uint ret;
            int lpType = new int();
            byte[] lpData = new byte[512];
            int lpcbData = 512;
            string SmartPhoneClientPath = "";
            UnmanagedAPI.CeRapiInit();
            ret = UnmanagedAPI.CeRegOpenKeyEx(UnmanagedAPI.HKEY_LOCAL_MACHINE, "Software\\MSRI\\", 0, 1, out key);
            if (ret == 0)
            {
                ret = UnmanagedAPI.CeRegQueryValueEx(key, string.Empty, 0, ref lpType, lpData, ref lpcbData);
                if (ret == 0)
                    SmartPhoneClientPath = System.Text.Encoding.Unicode.GetString(lpData, 0, lpcbData).Replace("\0","");
            }
            UnmanagedAPI.CeRegCloseKey(key);
            UnmanagedAPI.CeRapiUninit();
            return SmartPhoneClientPath;
        }


    }
    public class UnmanagedAPI
    {
        //PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
        //CeCreateProcess("myapp.exe", String.Empty, IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, ref pi)

        [DllImport("rapi.dll")]
        public static extern int CeCloseHandle(int hObject);

        [DllImport("rapi.dll")]
        public static extern int CeRapiInit();


        [DllImport("rapi.dll")]
        public static extern int CeRapiUninit();


        [DllImport("rapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        internal extern static int CeCreateProcess(
            string pszImageName,
            string pszCmdLine,
            IntPtr psaProcess,
            IntPtr psaThread,
            int fInheritHandles,
            int fdwCreate,
            IntPtr pvEnvironment,
            IntPtr pszCurDir,
            IntPtr psiStartInfo,
            ref PROCESS_INFORMATION pi);

        [DllImport("rapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern uint CeRegQueryValueEx(
            uint hKey,
            string lpValueName,
            int lpReserved,
            ref int lpType,
            byte[] lpData,
            ref int lpchData);

        [DllImport("rapi.dll", CharSet = CharSet.Unicode)]
        public static extern uint CeRegOpenKeyEx(
            uint HKEY,
            string lpSubKey,
            int ulOptions,
            uint samDesired,
            out uint phkResult);

        [DllImport("rapi.dll")]
        public static extern int CeRegCloseKey(uint hkey);

        //[DllImport("rapi.dll", EntryPoint = "CeRegQueryValueEx")]
        //public static extern uint CeRegQueryValueExLong(
        //    long hkey,
        //    long lpValueName,
        //    long lpReserved,
        //    long lpType,
        //    long lpdata,
        //    long lpcbData);

        //[DllImport("rapi.dll", EntryPoint = "CeRegQueryValueEx")]
        //public static extern uint CeRegQueryValueExString(
        //    long hkey,
        //    long lpValueName,
        //    long lpReserved,
        //    long lpType,
        //    long lpdata,
        //    long lpcbData);
    

        public const uint HKEY_CLASSES_ROOT = 0x80000000;
        public const uint HKEY_CURRENT_USER = 0x80000001;
        public const uint HKEY_LOCAL_MACHINE = 0x80000002;
        public const uint HKEY_USERS = 0x80000003;

    }
}
